package com.slimbloody.controller;

import com.google.protobuf.InvalidProtocolBufferException;
import com.slimbloody.hbase.HBaseTableInfoEnum;
import com.ysb.hbase.dml.HbaseDmlService;
import com.ysb.hbase.model.HbaseGetParam;
import com.ysb.hbase.model.HbasePutParam;
import com.ysb.hbase.model.ValueModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/tcm")
public class TcmController {
  @Autowired
  private HbaseDmlService hbaseDmlService;

  @Autowired
  private ApplicationContext applicationContext;

  public String fkey = Integer.valueOf(1705505).toString() + "@" + "党参";

  public String avgKey = "党参";

  // @PostMapping("/getUserFeature")
  // public void getUserFeature(@RequestBody String key) throws InvalidProtocolBufferException {
  //   ValueModel value = hbaseDmlService.getValue(
  //     new HbaseGetParam(
  //       HBaseTableInfoEnum.UserTcmQualityFeature.getTableName(),
  //       HBaseTableInfoEnum.UserTcmQualityFeature.getCf(),
  //       Optional.ofNullable(key).orElse(fkey),
  //       HBaseTableInfoEnum.UserTcmQualityFeature.getQualifier()
  //     )
  //   );
  //   byte[] v = value.getValue();
  //   TcmQualityFeature.UserTcmQualityFeature userTcmQualityFeature = TcmQualityFeature.UserTcmQualityFeature.parseFrom(v);
  //   log.info("userTcmQualityFeature: {}", userTcmQualityFeature);
  // }
  //
  // @PostMapping("/buildUserFeature")
  // public void buildUserFeature() {
  //   // storeId = 1705505
  //   TcmQualityFeature.FeatureItem.Builder ib1 = TcmQualityFeature.FeatureItem.newBuilder();
  //   ib1.setCnName("党参");
  //   ib1.setOrigin("河北");
  //   TcmQualityFeature.UserPricePref.Builder pb1 = TcmQualityFeature.UserPricePref.newBuilder();
  //   pb1.setLpref(0);
  //   pb1.setMpref(40);
  //   pb1.setHpref(60);
  //   TcmQualityFeature.UserPricePref p1 = pb1.build();
  //   ib1.setUpf(p1);
  //   TcmQualityFeature.FeatureItem item1 = ib1.build();
  //
  //   TcmQualityFeature.FeatureItem.Builder ib2 = TcmQualityFeature.FeatureItem.newBuilder();
  //   ib2.setCnName("党参");
  //   ib2.setOrigin("甘肃");
  //   TcmQualityFeature.UserPricePref.Builder pb2 = TcmQualityFeature.UserPricePref.newBuilder();
  //   pb2.setLpref(20);
  //   pb2.setMpref(40);
  //   pb2.setHpref(40);
  //   TcmQualityFeature.UserPricePref p2 = pb2.build();
  //   ib1.setUpf(p2);
  //   TcmQualityFeature.FeatureItem item2 = ib2.build();
  //
  //   TcmQualityFeature.UserTcmQualityFeature.Builder fb = TcmQualityFeature.UserTcmQualityFeature.newBuilder();
  //   fb.addItem(item1);
  //   fb.addItem(item2);
  //   TcmQualityFeature.UserTcmQualityFeature feature = fb.build();
  //
  //   boolean success = hbaseDmlService.putData(
  //     new HbasePutParam(
  //       HBaseTableInfoEnum.UserTcmQualityFeature.getTableName(),
  //       HBaseTableInfoEnum.UserTcmQualityFeature.getCf(),
  //       fkey,
  //       Arrays.asList(new ValueModel(HBaseTableInfoEnum.UserTcmQualityFeature.getQualifier(), feature.toByteArray()))
  //     )
  //   );
  //
  //   if (success) {
  //     System.out.println("buildUserFeature success");
  //   }
  // }
  //
  //
  // @PostMapping("/getAvg")
  // public void getAvg() throws InvalidProtocolBufferException {
  //   ValueModel value = hbaseDmlService.getValue(
  //     new HbaseGetParam(
  //       HBaseTableInfoEnum.TcmAvgPrice.getTableName(),
  //       HBaseTableInfoEnum.TcmAvgPrice.getCf(),
  //       avgKey,
  //       HBaseTableInfoEnum.TcmAvgPrice.getQualifier()
  //     )
  //   );
  //   byte[] v = value.getValue();
  //   TcmQualityFeature.TcmAvgPrice tcmAvgPrice = TcmQualityFeature.TcmAvgPrice.parseFrom(v);
  //   log.info("tcmAvgPrice: {}", tcmAvgPrice);
  //   log.info("name: {}", tcmAvgPrice.getItems(0).getCnName());
  // }
  //
  // @PostMapping("/buildAvg")
  // public void buildAvg() {
  //   // 6992613
  //   /*
  //   {
  //     "id": 6992613,
  //     "spec_unit": "500g",
  //     "unit": "袋",
  //     "prod_place": "河北",
  //     "druginfo_common_name": "党参",
  //     "druginfo_cn_name": "党参"
  //   }
  //    */
  //   TcmQualityFeature.TcmItemAvgPrice.Builder itemBuilder = TcmQualityFeature.TcmItemAvgPrice.newBuilder();
  //   itemBuilder.setWeight(500);
  //   itemBuilder.setUnit("袋");
  //   itemBuilder.setOrigin("河北");
  //   itemBuilder.setCnName("党参");
  //   itemBuilder.setLmThreshold(100);
  //   itemBuilder.setMuThreshold(400);
  //   TcmQualityFeature.TcmItemAvgPrice avgPrice1 = itemBuilder.build();
  //
  //   // 11099415
  //   /*
  //   {
  //     "id": 11099415,
  //     "spec_unit": "100g",
  //     "unit": "袋",
  //     "prod_place": "甘肃",
  //     "druginfo_common_name": "党参",
  //     "druginfo_cn_name": "党参"
  //   }
  //    */
  //
  //   itemBuilder.setWeight(100);
  //   itemBuilder.setUnit("袋");
  //   itemBuilder.setOrigin("甘肃");
  //   itemBuilder.setCnName("党参");
  //   itemBuilder.setLmThreshold(100);
  //   itemBuilder.setMuThreshold(400);
  //   TcmQualityFeature.TcmItemAvgPrice avgPrice2 = itemBuilder.build();
  //
  //   TcmQualityFeature.TcmAvgPrice.Builder builder = TcmQualityFeature.TcmAvgPrice.newBuilder();
  //   builder.addItems(avgPrice1);
  //   builder.addItems(avgPrice2);
  //   TcmQualityFeature.TcmAvgPrice tcmAvgPrice = builder.build();
  //
  //   boolean success = hbaseDmlService.putData(
  //     new HbasePutParam(
  //       HBaseTableInfoEnum.TcmAvgPrice.getTableName(),
  //       HBaseTableInfoEnum.TcmAvgPrice.getCf(),
  //       avgKey,
  //       Arrays.asList(new ValueModel(HBaseTableInfoEnum.TcmAvgPrice.getQualifier(), tcmAvgPrice.toByteArray()))
  //     )
  //   );
  //
  //   if (success) {
  //     System.out.println("buildAvg success");
  //   }
  // }
}
